package com.wehealth;


import java.util.ArrayList;
import java.util.List;

import com.wehealth.adapter.Adapter_questionnaire_page2;
import com.wehealth.dm.DM_Questionnaire_page2_item;
import com.wehealth.util.QuestionnaireDataManager;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;


@SuppressLint("ValidFragment")
public class Questionnaire_page2 extends Fragment {

	private MainActivity context;
    private List<DM_Questionnaire_page2_item>  mItemList;
	private QuestionnaireDataManager  mQuestionnaireDataManage;

	
	private int[] mitemNamesId = {
			R.string.questionaire_page2_item_hypertension,
			R.string.questionaire_page2_item_diabetes,
			R.string.questionaire_page2_item_coronary,
			R.string.questionaire_page2_item_cerebral,
			R.string.questionaire_page2_item_tumour,
			R.string.questionaire_page2_item_hyperlipemia,
			R.string.questionaire_page2_item_hyperuricemia
			};
	
	private int[] mitemNamesId2 = {
			R.string.questionaire_page2_item_hypertension1,
			R.string.questionaire_page2_item_diabetes1,
			R.string.questionaire_page2_item_coronary1,
			R.string.questionaire_page2_item_cerebral1,
			R.string.questionaire_page2_item_tumour1,
			R.string.questionaire_page2_item_hyperlipemia1,
			R.string.questionaire_page2_item_hyperuricemia1
			};

	public  Questionnaire_page2()
	{
		Log.i("init:", "Message"); 

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		return  inflater.inflate(R.layout.questionnaire_page2, null);
	}
	
	
	
	public void onActivityCreated(Bundle savedInstanceState){
		super.onActivityCreated(savedInstanceState);
		
		context =(MainActivity)getActivity();

		mQuestionnaireDataManage = QuestionnaireDataManager.getInstance();

		//mQuestionnaireDataManage = QuestionnaireDataManager.getInstance();
		
		TextView title=(TextView)context.findViewById(R.id.header_title);
		String titlename = context.getString(R.string.questionnaire_title);
		title.setText(titlename);
		Button bBack = (Button) context.findViewById(R.id.header_bBack);
		bBack.setVisibility(View.GONE);
		
		
		
		ListView mListview = (ListView)context.findViewById(R.id.questionnair_page2_list);
		mItemList = new ArrayList<DM_Questionnaire_page2_item>();
		
		int lenght = mitemNamesId.length;
		for(int i =0 ; i < lenght; i++){
			DM_Questionnaire_page2_item  item = new DM_Questionnaire_page2_item();
			String titleName = getString(mitemNamesId[i]);
			item.setTitle(titleName);
			String name = getString(mitemNamesId2[i]);
			item.setName(name);
			mItemList.add(item);
		}
		
		Adapter_questionnaire_page2  adpater = new Adapter_questionnaire_page2(context,mItemList);
		mListview.setAdapter(adpater);
		
	
		
		Button questionnaire_page1_next_bt = (Button)context.findViewById(R.id.questionnaire_page2_next_bt);
		questionnaire_page1_next_bt.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				openDialog();
			}
		});

	}

	
	private void openDialog(){
		        AlertDialog.Builder builder = new AlertDialog.Builder(context); 
		        builder.setIcon(null);
		        
		        String  names = "";
		        int  count =0;
		        int score2 = 50 ;
		        int length = mItemList.size();
		        for(int i = 0 ; i < length; i++){
		        	if(mItemList.get(i).getCheckValue() ==DM_Questionnaire_page2_item.CHECK_YES &&
		        			mItemList.get(i).isdiabetes2==false){
		        		names = mItemList.get(i).getName()+"、"+names;
		        		count ++;
		        		
		        	}else{
		        		if(mItemList.get(i).isdiabetes2==true &&
		        				mItemList.get(i).getCheckValue() ==DM_Questionnaire_page2_item.CHECK_YES ){
		        			score2= score2 - 50;
		        		}else{
		        		    Log.d("SIMMON"," i="+i);
		    
		        			score2 =score2+50;
		    		        Log.d("SIMMON"," score2="+score2);
		        		}
		        	}
		        }
		        mQuestionnaireDataManage.setScore2(score2);
		        
		        
		        String dialog_str =getString(R.string.questionaire_page2_dialog0);
		        if(count !=0){
		        	dialog_str=getString(R.string.questionaire_page2_dialog1)+names+getString(R.string.questionaire_page2_dialog2);
		        }
		        TextView textview = new TextView(context);
		         textview.setText(dialog_str);
		         textview.setTextColor(Color.BLACK);
		         textview.setTextSize(16);
		         textview.setPadding(5, 5, 5, 5);

		         textview.setGravity(Gravity.CENTER_VERTICAL);
		         builder.setView(textview);
		         
		         
		         builder.setPositiveButton("确定", new AlertDialog.OnClickListener() {
		             @Override
		             public void onClick(DialogInterface arg0, int arg1) {
		                 // TODO Auto-generated method stub
						   Questionnaire_page3 page3 = new Questionnaire_page3();
						   context.switchContent(page3);
		             }
		             
		         });
		         builder.create().show();
		         
		     }

}
